<template>
  <el-card>
    <el-table :data="skuTableData" border style="margin: 10px">
      <el-table-column type="index" label="序号" width="100px"> </el-table-column>
      <el-table-column prop="skuName" label="名称" show-overflow-tooltip width="180px">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="180px"> </el-table-column>
      <el-table-column prop="prop" label="图片" width="180px">
        <template #="{ row }">
          <img
            :src="row.skuDefaultImg"
            alt="暂无图片"
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="180px"> </el-table-column>
      <el-table-column prop="price" label="价格" width="180px"> </el-table-column>
      <el-table-column label="操作" width="280px" fixed="right">
        <template #="{ row }">
          <el-button
            type="success"
            :icon="row.isSale ? 'Bottom' : 'Top'"
            :title="row.isSale ? '下架' : '上架'"
            @click="onSaleOrCancelSale(row)"
          ></el-button>
          <el-button
            type="primary"
            icon="Edit"
            title="修改SKU"
            @click="updateSkuInfo"
          ></el-button>
          <el-button
            type="info"
            icon="InfoFilled"
            title="查看商品详细信息"
            @click="viewSkuInfo(row)"
          ></el-button>
          <el-button type="danger" icon="Delete" title="删除商品"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      background
      :page-sizes="[10, 20, 50]"
      layout="->, prev, pager, next, sizes, jumper, total"
      :total="total"
      @current-change="curPageChange"
      @size-change="pageSizeChange"
    >
    </el-pagination>
    <!-- 抽屉 -->
    <el-drawer v-model="drawer">
      <template #header>
        <h1>查看商品的详情</h1>
      </template>
      <template #default>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuDetail.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuDetail.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuDetail.price }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuDetail.skuAttrValueList"
              :key="item.id"
              type="success"
              style="margin: 5px 5px"
              >{{ item.valueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuDetail.skuSaleAttrValueList"
              :key="item.id"
              type="success"
              style="margin: 5px 5px"
              >{{ item.saleAttrValueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item v-for="item in skuDetail.skuImageList" :key="item.id">
                <img
                  :src="item.imgUrl"
                  alt="暂无图片"
                  style="width: 100%; height: 100%"
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import {
  reqSkuInfo,
  reqSkuOnSale,
  reqSkuCancelSale,
  reqSkuDetail,
} from "@/api/product/sku";
import type { SkuObj } from "@/api/product/sku/types";
import { ElMessage } from "element-plus";
// 当前页
const currentPage = ref(1);
// 页面展示数据条数
const pageSize = ref(10);
// 数据总数
const total = ref(0);
// 表格展示的SKU数据
const skuTableData = ref<SkuObj[]>([]);
// 控制抽屉的显示和隐藏
const drawer = ref(false);
// SKU的详细信息
const skuDetail = ref<SkuObj>({
  category3Id: "", // 三级分类Id
  spuId: "",
  tmId: "",
  skuName: "",
  price: "",
  weight: "",
  skuDesc: "",
  skuImageList: [],
  skuAttrValueList: [], // 平台属性的收集
  skuSaleAttrValueList: [], // 销售属性
  skuDefaultImg: "",
});

onMounted(() => {
  getSkuInfo();
});
// 获取SKU数据
const getSkuInfo = async (curPage = 1) => {
  currentPage.value = curPage;
  let result = await reqSkuInfo(currentPage.value, pageSize.value);
  if (result?.code === 200) {
    skuTableData.value = result.data?.records;
    total.value = result.data?.total;
  } else {
    ElMessage.error("获取Sku数据失败");
  }
};
// 当前页改变事件回调（会传入当前页数据）
const curPageChange = () => {
  getSkuInfo(currentPage.value);
};
// 页面展示数据个数变化回调
const pageSizeChange = () => {
  getSkuInfo();
};
// 商品上级与下架
const onSaleOrCancelSale = async (row: SkuObj) => {
  if (row.isSale === 1) {
    let result = await reqSkuCancelSale(row.id as number);
    if (result?.code === 200) {
      ElMessage.success("下架成功");
      getSkuInfo(currentPage.value);
    } else {
      ElMessage.error("下架失败");
    }
  }
  if (row.isSale === 0) {
    let result = await reqSkuOnSale(row.id as number);
    if (result?.code === 200) {
      ElMessage.success("上架成功");
      getSkuInfo(currentPage.value);
    } else {
      ElMessage.error("上架失败");
    }
  }
};
// 更新SKU信息按钮回调
const updateSkuInfo = () => {
  ElMessage.info("程序员正在努力编写中");
};
// 查看Sku详细信息按钮的回调
const viewSkuInfo = async (row: SkuObj) => {
  // 显示抽屉
  drawer.value = true;
  // 获取数据
  let result = await reqSkuDetail(row.id as number);
  if (result?.code === 200) {
    skuDetail.value = result?.data;
  } else {
    ElMessage.error("获取商品详细信息失败");
  }
};
</script>

<style scoped lang="scss">
:deep(.el-drawer__header.el-drawer__header) {
  margin-bottom: 10px !important;
}
// :deep(.el-drawer__body) {
//   overflow: hidden !important;
// }
.el-carousel {
  .el-carousel__item {
    &:nth-child(2n) {
      background-color: red;
    }
    &:nth-child(2n + 1) {
      background-color: blue;
    }
  }
}
</style>
